package com.linkivr.action;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import net.sf.ezmorph.bean.MorphDynaBean;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.stereotype.Component;

import com.linkivr.util.GL;
import com.linkivr.model.*;
import com.linkivr.service.*;


@Component("groupAction")
public class GroupAction extends MyAction {

	private static final long serialVersionUID = 1L;
	private GroupService groupService;

	public GroupService getService() {
		return groupService;
	}

	@Resource
	public void setService(GroupService groupService) {
		this.groupService = groupService;
	}

	public String list() throws IOException {
		GL.info("list()函数  ");
		beginReqParam();

		List<T_group> groups = groupService.queryAll(start, limit);
		jsonString = "{totalProperty:" + groupService.queryCount() + ",root:[";
		int i = 0;
		for (T_group o : groups) {

			if (i != 0 && i != groups.size())
				jsonString = jsonString + ",";
			i++;
			String t = "{group_id:'" + o.getGroup_id() + "',group_name:'" + o.getGroup_name() + "',group_authority:'"
					+ o.getGroup_authority() + "'}";
			jsonString = jsonString + t;
		}
		jsonString += "]}";
		out.write(jsonString);
		GL.info(jsonString);
		GL.info("start=" + start + ",limit=" + limit);

		endReqParam();
		return null;
	}

	public String listAll() throws Exception {
		GL.info("listAll()函数  ");
		beginReqParam();

		List<T_group> complist = groupService.queryAll();
		JSONArray array = JSONArray.fromObject(complist);
		out.write(array.toString());

		endReqParam();
		return null;
	}

	public String query() throws IOException {
		GL.info("query()函数  ");
		beginReqParam();

		String p_id = request.getParameter("p_id");

		T_group tabObj = groupService.query(Integer.parseInt(p_id));
		GL.info("query()函数 ok p_id= " + p_id);
		jsonString = "{success: true ,data:{f_group_name:'" + tabObj.getGroup_name() + "',f_group_authority:'"
				+ tabObj.getGroup_authority() + "'}}";
		out.write(jsonString);
		GL.info(jsonString);

		endReqParam();
		return null;
	}

	public String add() throws IOException {
		GL.info("add()函数  ");
		String jsonString = "";
		beginReqParam();

		// String f_group_id = request.getParameter("f_group_id");
		String f_group_name = request.getParameter("f_group_name");
		int f_group_authority = Integer.parseInt(request.getParameter("f_group_authority"));

		T_group group = new T_group();
		group.setGroup_name(f_group_name);
		group.setGroup_authority(f_group_authority);

		groupService.add(group);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

	public String delete() throws IOException {
		GL.info("delete()函数  ");
		String jsonString = "";
		beginReqParam();

		String f_data = request.getParameter("f_data");
		JSONObject a = null;
		Object bean = null;
		JSONArray array = JSONArray.fromObject(f_data);
		Object[] objArray = array.toArray();
		for (Object obj : objArray) {
			a = JSONObject.fromObject(obj);
			bean = JSONObject.toBean(a);
			// 经过查看，发现 bean 为 MorphDynaBean 类型
			MorphDynaBean morphDynaBean = (MorphDynaBean) bean;
			String id = (String) morphDynaBean.get("group_id");
			groupService.delete(Integer.parseInt(id));

		}
		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);

		endReqParam();
		return null;
	}

	public String update() throws IOException {
		GL.info("update()函数  ");
		String jsonString = "";
		beginReqParam();

		String p_id = request.getParameter("p_id");

		String f_group_name = request.getParameter("f_group_name");
		int f_group_authority = Integer.parseInt(request.getParameter("f_group_authority"));

		T_group group = groupService.query(Integer.parseInt(p_id));

		group.setGroup_name(f_group_name);
		group.setGroup_authority(f_group_authority);

		groupService.update(group);

		jsonString = "{success: true ,msg:'操作成功'}";
		out.write(jsonString);
		GL.info(jsonString);
		endReqParam();
		return null;
	}

}
